package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.smart.village.big.screen.mapper.VolunteerTeamsMapper;
import com.smart.village.big.screen.service.VolunteerTeamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class VolunteerTeamsServiceImpl implements VolunteerTeamsService {
    @Autowired
    private VolunteerTeamsMapper volunteerTeamsMapper;
    @Override
    public JSONObject getVolunteer(String partyOrganizationSerialNumber) {
        List<HashMap> volunteerTeamList =  volunteerTeamsMapper.getVolunteer(partyOrganizationSerialNumber);
        int total = volunteerTeamList.size();
        int activityCnt = 0;
        int memberCnt = 0;

        for (HashMap volunteerTeam : volunteerTeamList) {
            activityCnt += Integer.parseInt(volunteerTeam.get("activityCount").toString());
            memberCnt += Integer.parseInt(volunteerTeam.get("memberCount").toString());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("activityCnt", activityCnt);
        jsonObject.put("memberCnt", memberCnt);
        jsonObject.put("total", total);
        jsonObject.put("volunteerTeamList", volunteerTeamList);
        return jsonObject;
    }

    @Override
    public JSONObject getVolunteerDetail(String volunteerTeamSerialNumber) {
        HashMap volunteerMap = volunteerTeamsMapper.getVolunteerDetail(volunteerTeamSerialNumber);
        JSONObject jsonObject = new JSONObject();
        if(ObjectUtils.isEmpty(volunteerMap)){
            jsonObject.put("name", "");
            jsonObject.put("member", "");
            jsonObject.put("introduce", "");
            jsonObject.put("activityList", Lists.newArrayList() );
        }else{
            jsonObject.put("name", volunteerMap.containsKey("name")?volunteerMap.get("name").toString():"");
            jsonObject.put("captainName", volunteerMap.containsKey("captainName")?volunteerMap.get("captainName").toString():"");
            jsonObject.put("introduce", volunteerMap.containsKey("introduce")?volunteerMap.get("introduce").toString():"");
            List<HashMap> activityList = volunteerTeamsMapper.getVolunteerActivityList(volunteerTeamSerialNumber);
            jsonObject.put("activityList",activityList );
        }

        return jsonObject;
    }

    @Override
    public JSONObject getVolunteerTeamsActivitiesDetail(String volunteerTeamsActivitiesSerialNumber) {
       return volunteerTeamsMapper.getVolunteerTeamsActivitiesDetail(volunteerTeamsActivitiesSerialNumber);

    }
}
